//
//  WaterGrainView.m
//  水波纹
//
//  Created by bia一下 on 2017/4/19.
//  Copyright © 2017年 bia一下. All rights reserved.
//
#define mRGBColor(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

#import "WaterGrainView.h"

@interface WaterGrainView ()

@property (nonatomic, strong) UIColor *lineColor;

@property (nonatomic, assign) CGFloat lineWidth;

@end

@implementation WaterGrainView

-(id)initWithFrame:(CGRect)frame lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth
{
    self = [super initWithFrame:frame];
    self.lineColor = lineColor;
    self.lineWidth = lineWidth;
    return self;
}

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self.layer setBackgroundColor:[UIColor clearColor].CGColor];
    //根据当前view的rect绘制矩形
//    UIRectFill(rect);
    //循环次数
    NSInteger pulsingCount = 5;
    //动画时间
    double animationDuration = 3;
    //创建图层
    CALayer * animationLayer = [CALayer layer];
    [animationLayer setBackgroundColor:(__bridge CGColorRef _Nullable)([UIColor whiteColor])];
    for (int i = 0; i < pulsingCount; i++) {
        CALayer * pulsingLayer = [CALayer layer];
        //创建图层大小
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        //修改图层的颜色
        pulsingLayer.borderColor = _lineColor.CGColor;
        //修改图层的宽度
        pulsingLayer.borderWidth = _lineWidth;
        //修改图层的圆角
        pulsingLayer.cornerRadius = rect.size.height / 2;
        
        //速度控制器
        CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
        
        //组动画
        CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBackwards;
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / (double)pulsingCount;
        animationGroup.duration = animationDuration;
        animationGroup.repeatCount = HUGE;
        animationGroup.timingFunction = defaultCurve;
        
        //注册动画
        CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //改变起始值
        scaleAnimation.fromValue = @1.4;
        //改变结束值
        scaleAnimation.toValue = @2.2;
        
        CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        //关键帧绘制
        opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
        //关键帧时间
        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
        //添加动画
        animationGroup.animations = @[scaleAnimation, opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
        [animationLayer addSublayer:pulsingLayer];
    }
    [self.layer addSublayer:animationLayer];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
